<!--
 * @Author: suo.tianbao suo.tianbao@vpclub.cn
 * @Date: 2023-09-01
 * @LastEditors: suo.tianbao suo.tianbao@vpclub.cn
 * @LastEditTime: 2023-09-01
 * @FilePath: /element-admin/src/views/GitUsers.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div id="Member-Level-Point-Page">
    <!--顶部搜索栏-->
    <div class="col-md-12">
      <el-row :gutter="20">
        <el-col :offset="1" :span="22" class="clearfix">
          <el-form :inline="true" :model="query" class="demo-form-inline">
            <el-form-item label="关键词">
              <el-input v-model="query.key" :clearable="true" placeholder="会员ID & 名称 & 手机号"
                        size="small"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button class="register_btn" size="small" type="primary" @click="handleFetch">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button class="register_btn" size="small" type="success" @click="dialogVisible = true">添加</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <!--中部数据表格栏-->
    <div class="col-md-12">
      <el-row :gutter="20">
        <el-col :offset="1" :span="22" class="clearfix">
          <el-table :key="isIndex"
                    v-loading="isLoading"
                    :cell-style="{padding:'3px'}"
                    :data="memPointList" :height="tableHeight" :row-style="{height:'30px'}" border row-key="id" stripe
                    style="width: 100%">
            <el-table-column v-if="isIndex == 1" fixed label="序号" type="index" width="75"></el-table-column>
            <el-table-column fixed label="id" prop="id" v-if="false"/>
            <el-table-column fixed label="需要积分数" prop="points">
              <template v-slot="scope">
                <el-input v-model="scope.row.points" :clearable="true" placeholder="积分数" size="small"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="等级名称" prop="level">
              <template v-slot="scope">
                <el-input v-model="scope.row.level" :clearable="true" placeholder="等级名称" size="small"></el-input>
              </template>
            </el-table-column>
            <el-table-column :width="optCulWidth" label="操作">
              <template v-slot="scope">
                <el-button plain size="mini" type="primary" @click="handleSave(scope.row)">保存</el-button>
                <el-button plain size="mini" type="danger" @click="handleDel(scope.row)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <el-col :offset="1" :span="22" class="custRow clearfix">
          <el-pagination
              :current-page="currentPage"
              :page-count=10
              :page-size="query.pageSize"
              :page-sizes="[20, 50, 100]"
              :pager-count=5
              :total="query.total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange">
          </el-pagination>
        </el-col>
      </el-row>
    </div>
    <div>
      <el-dialog :before-close="handleClose" :visible.sync="dialogVisible" :width="dialogWidth">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span style="font-size: 22px;">会员积分等级配置 </span>
            </div>
          </template>
          <el-form :model="MemLevel" label-width="140px">
            <el-input v-if="false" v-model="MemLevel.id" size="small"/>
            <el-form-item label="需要积分数">
              <el-col :lg="8" :md="8" :sm="8" :xl="8" :xs="8">
                <el-input v-model="MemLevel.points" :clearable="true" placeholder="需要积分数" size="small"/>
              </el-col>
            </el-form-item>
            <el-form-item label="等级名称">
              <el-col :lg="8" :md="8" :sm="8" :xl="8" :xs="8">
                <el-input v-model="MemLevel.level" :clearable="true" placeholder="等级名称" size="small"/>
              </el-col>
            </el-form-item>
          </el-form>
        </el-card>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAdd">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {mapMutations} from "vuex";
import {add, drop, fetch, update,} from "@/api/MemberLevel";

export default {
  name: "MemberLevelView",
  components: {},
  data() {
    return {
      isIndex: 1,
      isLoading: true,
      currentPage: 1,
      total: 0,
      optCulWidth: 300,
      query: {
        key: '',
        pageSize: 20,
        pageNum: 1,
      },
      MemLevel: {
        id: 0,
        points: 0,
        level: "",
      },
      tableHeight: 650,
      memPointList: [],
      dialogWidth: "1240px",
      dialogVisible: false,
      memId: '',
    }
  },
  computed: {},
  methods: {
    ...mapMutations(['add_tab']),
    init_obj() {
      return {
        id: 0,
        points: 0,
        level: "",
      }
    },
    handleFetch() {
      this.isLoading = true;
      fetch(this.query).then(res => {
        let _res = eval(res.data);
        // 刷新页面表格
        this.memPointList = _res.lists;
        this.currentPage = _res.currentPage;
        this.query.pageSize = _res.pageSize;
        this.query.total = _res.total;
        this.isLoading = false;
      }).catch(err => {
        this.isLoading = false;
        this.$message.error('错了哦，这是一条错误消息' + err);
      });
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange(size) {
      this.query.pageSize = size;
      this.handleFetch(this.query);
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.query.pageNum = currentPage;
      this.handleFetch(this.query);
    },
    handleAdd() {
      add({data: this.MemLevel}).then((result) => {
        if (result.code == 200) {
          this.$notify({title: '执行成功', message: '数据操作完成', type: 'success', offset: 100});
          this.dialogVisible = false;
          this.handleFetch();
          this.MemLevel = this.init_obj();
        }
      })
    },
    handleSave(row) {
      this.MemLevel.id = row.id;
      this.MemLevel.points = row.points;
      this.MemLevel.level = row.level;
      update({id: row.id, data: this.MemLevel}).then((result) => {
        if (result.code == 200) {
          this.$notify({title: '执行成功', message: '数据操作完成', type: 'success', offset: 100});
          this.dialogVisible = false;
          this.handleFetch();
          this.MemLevel = this.init_obj();
        }
      })
    },
    handleDel(row) {
      let param = {"id": row.id,}
      drop(param).then(res => {
        if (res.code == 200) {
          this.handleFetch();
          this.$notify({title: '执行成功', message: '数据操作完成', type: 'success', offset: 100});
        }
      }).catch(err => {
        this.$notify({title: '执行失败', message: '数据操作异常' + err, type: 'error', offset: 100});
      });
    },
    handleClose() {
      this.dialogVisible = false
    }
  },
  created() {
    this.handleFetch();
  },
  mounted() {
  },
}
</script>
<style scoped>

.custRow {
  display: flex;
  margin-top: 5px;
  padding: 5px 5px 5px 5px;
}
</style>